package com.eShopManager.intigration.ebay;

import com.ebay.sdk.ApiContext;
import com.ebay.sdk.ApiException;
import com.ebay.sdk.SdkException;
import com.ebay.sdk.call.FetchTokenCall;
import com.ebay.sdk.call.GetCategoriesCall;
import com.ebay.sdk.call.GetSessionIDCall;
import com.ebay.soap.eBLBaseComponents.SiteCodeType;

public class EbayFacade {
	private ApiContextCache apiContextCache;

	public void setApiContextCache(ApiContextCache apiContextCache) {
		this.apiContextCache = apiContextCache;
	}

	public String getSessionID() {
		ApiContext apiContext = apiContextCache.getApiContext();
		GetSessionIDCall api = new GetSessionIDCall(apiContext);
		api.setRuName(apiContextCache.getRuname());
		try {
			return api.getSessionID();
		} catch (ApiException e) {
			e.printStackTrace();
		} catch (SdkException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String fetchToken(String sessionID) {
		ApiContext apiContext = apiContextCache.getApiContext();
		FetchTokenCall api = new FetchTokenCall(apiContext);
		api.setSessionID(sessionID);

		try {
			return api.fetchToken();
		} catch (ApiException e) {
			e.printStackTrace();
		} catch (SdkException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public void getCategorys(String token) {
		ApiContext apiContext = apiContextCache.getApiContext();
		apiContext.getApiCredential().seteBayToken(token);
		GetCategoriesCall api = new GetCategoriesCall(apiContext);
		api.setCategorySiteID(SiteCodeType.US);
		api.setViewAllNodes(true);
		try {
			System.out.println(api.getCategories());
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SdkException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
